<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/27
 * Time: 9:45
 */

namespace app\order\controller;


use cmf\controller\BaseController;

interface ILock
{
    const EXPIRE=5;
    public function getLock($key,$timeout=self::EXPIRE);
    public function releaseLock($key);

}

class SelectSeatController extends BaseController implements ILock
{
    private $_fp;
    private $_single;
    public function __construct($options)
    {
        parent::__construct();
        if(isset($options['path']) && is_array($options))
        {
            $this->_lockPath=$options['path'];
        }else{
            $this->_lockPath='/tmp/';
        }
        $this->_single=$options['single']?$options['single']:false;
    }
    public function getLock($key, $timeout = self::EXPIRE)
    {
        // TODO: Implement getLock() method.
        $startTime =time();

        $file=md5(__FILE__.$key);
        $this->_fp=fopen($this->_lockPath.'.lock','w+');
        if(true || $this->_single)
        {
            $op=LOCK_EX+LOCK_NB;
        }else{
            $op=LOCK_EX;
        }
        if (false == flock($this->_fp, $op, $a))
         {
            throw new Exception('failed');
        }
        dump(1111);
        return true;

    }
    public function releaseLock($key)
    {
        // TODO: Implement releaseLock() method.
        flock($this->_fp,LOCK_UN);
        fclose($this->_fp);
        dump(123);
    }
}